package cn.ydxiaoshuai.ai.chat.controller;

import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;



/**
 * @ClassName ERNIEController
 * @description:
 * @author: 小帅丶
 * @create: 2025-09-19 13:37
 * @Version 1.0
 **/
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/baidu")
public class ERNIEController {

    private final ChatClient ERNIEClient;

    @RequestMapping(value = "/chat",produces = "text/html;charset=utf-8")
    public Flux<String> chat(@RequestParam(value = "content",required = false) String content) {
        try {
            return textChat(content);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private Flux<String> textChat(String prompt) {
        if(StrUtil.isEmpty(prompt)){
            prompt = "你好，请介绍一下你自己";
        }
        return ERNIEClient.prompt()
                .user(prompt)
                .stream()
                .content();
    }
}
